<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生选课系统 - 登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            padding: 0;
        }
        .login-container {
            width: 350px;
            padding: 40px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }
        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        .error-message {
            color: red;
            margin-top: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h1>学生选课系统</h1>
        <div class="form-group">
            <label for="studentNo">学号</label>
            <input type="text" id="studentNo" placeholder="请输入学号">
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        <button onclick="login()">登录</button>
        <div id="error-message" class="error-message"></div>
    </div>

    <script>
        // 检查是否已登录
        const student = localStorage.getItem('student');
        if (student) {
            // 已登录，直接跳转到选课页面
            window.location.href = '/index.html';
        }
        
        function login() {
            const studentNo = document.getElementById('studentNo').value;
            const password = document.getElementById('password').value;
            
            if (!studentNo || !password) {
                document.getElementById('error-message').textContent = '学号和密码不能为空';
                return;
            }
            
            // 创建JSON数据
            const jsonData = {
                studentNo: studentNo,
                password: password
            };
            
            fetch('/student/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(jsonData)
            })
            .then(response => response.json())
            .then(result => {
                if (result.code === 200) {
                    // 登录成功，将学生信息存储到本地存储
                    localStorage.setItem('student', JSON.stringify(result.result));
                    // 跳转到选课页面
                    window.location.href = '/index.html';
                } else {
                    // 显示登录错误信息
                    document.getElementById('error-message').textContent = result.message;
                }
            })
            .catch(error => {
                document.getElementById('error-message').textContent = '登录请求失败: ' + error.message;
            });
        }
        
        // 监听回车键
        document.getElementById('password').addEventListener('keyup', function(event) {
            if (event.key === 'Enter') {
                login();
            }
        });
    </script>
</body>
</html> 